
import {
  RECEIVE_USERINFO,
  RESET_USERINFO
} from '../mutation-types'

import {
  reqUserbySession,
  reqLogout
} from 'api'

const state = {
  userinfo:{}
}

const actions = {
  async getUser({commit}){
    const userInfo = await reqUserbySession()
    if(userInfo.code===0){
      const userinfo = userInfo.data
      commit(RECEIVE_USERINFO,userinfo)
    }
  },
  async logOut({commit}){
    const userInfoInSession = await reqLogout()
    if(userInfoInSession.code===0){
      commit(RESET_USERINFO)
    }
  }
}

const mutations = {
  [RECEIVE_USERINFO](state,userinfo){
    state.userinfo = userinfo
  },
  [RESET_USERINFO](state){
    state.userinfo = {}
  }
}

const getters = {

}

export default {
  state,
  mutations,
  actions,
  getters,
}
